package com.wishu.finance.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wishu.finance.domain.ReceiptDto;
import com.wishu.finance.domain.ResponseDetailCfContractDto;
import com.wishu.finance.domain.TCfContract;
import com.wishu.finance.domain.TCwReceiptPlan;
import com.wishu.finance.enums.BusinessCostTypesEnum;
import com.wishu.finance.enums.ContractTypeEnum;
import com.wishu.finance.mapper.TCfContractMapper;
import com.wishu.finance.mapper.TCwReceiptPlanMapper;
import com.wishu.finance.service.TCfContractService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 25813
 * @description 针对表【t_cf_contract】的数据库操作Service实现
 * @createDate 2023-05-03 20:39:23
 */
@Service
public class TCfContractServiceImpl extends ServiceImpl<TCfContractMapper, TCfContract>
        implements TCfContractService {
    @Resource
    TCwReceiptPlanMapper tCwReceiptPlanMapper;

    @Override
    public ResponseDetailCfContractDto detailCfContract(String cfContractId) {
        //根据合同里的房源ID查询房源信息
        TCfContract contractRecord = baseMapper.selectSearchById(cfContractId);
        //创建一个新的对象
        ResponseDetailCfContractDto resultDto = new ResponseDetailCfContractDto();
        //把contractRecord里的数据拷贝到resultDto里
        BeanUtils.copyProperties(contractRecord, resultDto);
        // 第二步 查询收款计划表
        Map<String, Object> condition = new HashMap<String, Object>();
        // 合同ID
        condition.put("contract_id", cfContractId);
        // 合同类型：出房
        condition.put("contract_type", ContractTypeEnum.CF.getIndex());
        // 费用类型 ：租金
        condition.put("item_type", BusinessCostTypesEnum.ZUJIN.getIndex());
        //根据map查询收款计划数据集合
        List<TCwReceiptPlan> entityList = tCwReceiptPlanMapper.selectByMap(condition);
        //创建一个新的集合
        List<ReceiptDto> receiptList = new ArrayList<ReceiptDto>();
        ReceiptDto receiptDto = null;
        //判断是不是空
        if (null != entityList && entityList.size() > 0) {
            //遍历entityList
            for (TCwReceiptPlan record : entityList) {
                //new一个新的ReceiptDto，只需要所用字段
                receiptDto = new ReceiptDto();
                //把收款计划ID赋值给receiptDto
                receiptDto.setReceiptId(record.getId());
                //把剩下的东西拷贝到receiptDto里
                BeanUtils.copyProperties(record, receiptDto);
                //添加到集合里
                receiptList.add(receiptDto);
            }
        }
        //把集合赋值到resultDto里
        resultDto.setReceiptList(receiptList);
        //返回
        return resultDto;
    }
}




